package com.brother.geocode;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.brother.database.DataBase;
import com.brother.model.LatLngAddress;
import com.brother.model.Tracker;
import com.brother.service.TrackerService;

public class GeoCoder {
private static Logger log = Logger.getLogger(GeoCoder.class);
private static TrackerService tkSrv = new TrackerService();
public static LatLngAddress getAddress(String lat, String lng, String language){
	LatLngAddress address = null;
	String sql = "select lat,lng,address from cooraddress where language='"+language
	+"' and (lat between "+lat+"-0.0001 and "+lat
	+"+0.0001) and (lng between "+lng+"-0.0001 and "+lng+"+0.0001)";
	 try {
		 ResultSet rs = DataBase.query(sql);
		if(rs !=null && rs.next()){
			address = new LatLngAddress();
			address.setAddress(rs.getString("address")); 
			address.setLat(rs.getFloat("lat"));
			address.setLng(rs.getFloat("lng"));
		 }
	} catch (SQLException e) {
		log.error(e);
	}finally{
		DataBase.closeConnection();
	}
	return address;
}	

public static void saveAddress(String lat, String lng,String address, String language, String reportId,String trackerId){
	if(address == null){
		return;
	}
	address = address.replaceAll("\\\\", "\\\\\\\\");
	address = address.replaceAll("'", "\\\\'");
	String sql = "";
	if(getAddress(lat, lng, language) == null){
		sql = "insert into CoorAddress(lat,lng,address,createtime,language) values("+lat+","+lng+",'"+address+"',now(),'"+language+"')";
		DataBase.update(sql);
	}
	sql = "update realtimedata set address='"+address+"' where language='"+language
	+"' and (lat between "+lat+"-0.0001 and "+lat
	+"+0.0001) and (lng between "+lng+"-0.0001 and "+lng+"+0.0001)";
	DataBase.update(sql);
//	sql = "update report set address='"+address+"' where language='"+language+"' and abs(signed_lat-"+lat+")<0.0001 and abs(signed_lng-"+lng+")<0.0001";
	if(reportId!=null){
		Tracker t = tkSrv.getTrackerByFieldName("trackerId", trackerId);
		sql = "update r_"+t.getUserTrackerId()+" set address='"+address+"' where  reportId="+reportId;
		DataBase.update(sql);
	}
	
	DataBase.closeConnection();
}

public static void setReportAddress(String lat, String lng , String address, String locale, String reportId,String trackerId){
	if(address == null){
		return;
	}
	address = address.replaceAll("\\\\", "\\\\\\\\");
	address = address.replaceAll("'", "\\\\'");
	Tracker t = tkSrv.getTrackerByFieldName("trackerId", trackerId);
	String sql = "update r_"+t.getUserTrackerId()+" set address='"+address+"' where  reportId="+reportId;
	DataBase.update(sql);
}

public static void updateRealTimeData(String lat, String lng,String address, String language){
	String sql = "update realtimedata set address='"+address+"' where language='"+language+"' and abs(lat-"+lat+")<0.0001 and abs(lng-"+lng+")<0.0001";
	DataBase.update(sql);
	//sql = "update report set address='"+address+"' where language='"+language+"' and abs(lat-"+lat+")<0.0001 and abs(lng-"+lng+")<0.0001";
	//DataBase.update(sql);	
	DataBase.closeConnection();
}



}
